const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
  try {
    console.log('开始创建测试商家数据...')

    // 定义3个不同行业的测试商家
    const testShops = [
      {
        // 汽车服务商家
        shopName: "张师傅汽修",
        industryId: "auto_service",
        industryName: "汽车服务",
        industryIcon: "🚗",
        ownerOpenid: "test_owner_1",
        ownerPhone: "13999662338",
        address: "温宿县太古里小区22号楼1单元602室",
        businessHours: "08:00-18:00",
        description: "专业汽车维修保养，10年老店，技术精湛",
        services: [
          { name: "机油保养", price: 200, icon: "🔧" },
          { name: "美容洗车", price: 80, icon: "✨" },
          { name: "维修检测", price: 150, icon: "🔍" },
          { name: "轮胎服务", price: 120, icon: "⚙️" }
        ],
        status: "active",
        rating: 5.0,
        createTime: db.serverDate()
      },
      {
        // 理发店商家
        shopName: "时尚造型",
        industryId: "hair_salon",
        industryName: "理发店",
        industryIcon: "💇",
        ownerOpenid: "test_owner_2",
        ownerPhone: "13800138000",
        address: "温宿县人民路88号",
        businessHours: "09:00-21:00",
        description: "高端理发店，资深发型师，预约优先",
        services: [
          { name: "剪发", price: 50, icon: "✂️" },
          { name: "烫发", price: 200, icon: "🌀" },
          { name: "染发", price: 180, icon: "🎨" },
          { name: "护理", price: 100, icon: "💆" }
        ],
        status: "active",
        rating: 4.8,
        createTime: db.serverDate()
      },
      {
        // 餐厅商家
        shopName: "海鲜大酒楼",
        industryId: "restaurant",
        industryName: "餐厅",
        industryIcon: "🍽️",
        ownerOpenid: "test_owner_3",
        ownerPhone: "13700137000",
        address: "温宿县美食街99号",
        businessHours: "10:00-22:00",
        description: "正宗海鲜料理，环境优雅，包厢齐全",
        services: [
          { name: "小包厢(4-6人)", price: 500, icon: "🚪", capacity: 6 },
          { name: "中包厢(8-10人)", price: 800, icon: "🚪", capacity: 10 },
          { name: "大包厢(12-15人)", price: 1200, icon: "🚪", capacity: 15 }
        ],
        status: "active",
        rating: 4.9,
        createTime: db.serverDate()
      }
    ]

    console.log('准备添加3个测试商家...')

    // 先清空旧数据（如果有）
    const deleteResult = await db.collection('shops')
      .where({ ownerOpenid: db.command.in(['test_owner_1', 'test_owner_2', 'test_owner_3']) })
      .remove()
    
    console.log('删除旧测试数据:', deleteResult.removed)

    // 批量添加新数据
    const addTasks = testShops.map(shop => {
      return db.collection('shops').add({ data: shop })
    })

    const results = await Promise.all(addTasks)
    
    console.log('添加成功，商家数量:', results.length)

    return {
      success: true,
      message: `成功创建 ${testShops.length} 个测试商家`,
      shops: testShops.map(shop => ({
        name: shop.shopName,
        industry: shop.industryName,
        icon: shop.industryIcon
      }))
    }

  } catch (err) {
    console.error('创建失败:', err)
    return {
      success: false,
      error: err.message,
      stack: err.stack
    }
  }
}
